<template>
	<view>
		<totalMask :show.sync="isShow" :showType="showType">
			<view class="nvue-flex">
				<view class="title-bg">
					<text class="title">连麦列表</text>
				</view>

				<scroll-view scroll-y class="list" :show-scrollbar="false" @scrolltolower='onLoadMore'>
					<view class="view">
						<block v-for="(item, index) in list" :key="index">
							<view class="item">
								<image class="avatar" :src="item.user.avatar"></image>

								<view class="nickname-bg">
									<text class="nickname">{{item.user.nickname}}</text>
								</view>

								<view class="lm-btn" @click.stop="onVideoLM(item)">
									<text class="lm-title">视频连麦</text>
								</view>

								<view class="lm-btn yy-lm" @click.stop="onVoiceLM(item)">
									<text class="lm-title">语音连麦</text>
								</view>
							</view>
						</block>
					</view>
				</scroll-view>
			</view>
		</totalMask>
	</view>
</template>

<script>
	import storage from "@/utils/storage";
	import {
		getLiveUserList,
		zbApplyLM
	} from "@/api/newLive.js";
	import st from "@/utils/showtoast.js";
	
	export default {
		data() {
			return {
				isShow: false,
				showType: 0,
				
				page: 1,
				total: 0,
				list: [],
			}
		},
		destroyed() {
			
		},
		mounted() {
			uni.$on("ZBApplyLM", () => {
				this.isShow = true
				this.showType = 6
				
				this.onRefresh()
			});
			/**
			 * 物理返回键退出
			 */
			uni.$on("onAndroidBackPress", () => {
				this.onDestroyed()
			});
		},
		methods: {
			/**
			 * 销毁
			 */
			onDestroyed() {
				uni.$off("ZBApplyLM")
				uni.$off("onAndroidBackPress")
			},
			
			/**
			 * 视频连麦
			 */
			onVideoLM(item) {
				this.applyLMData(1, item.id)
			},
			/**
			 * 语音连麦
			 */
			onVoiceLM(item) {
				this.applyLMData(2, item.id)
			},
			/**
			 * 申请连麦接口
			 */
			async applyLMData(type, user_id) {
				let res = await zbApplyLM(storage.getLiveRoomInfo().id, type, user_id)
				st.show("申请成功，等待用户确认邀请")
			},
			
			/**
			 * 加载更多
			 */
			onLoadMore() {
				if (this.list.length < this.total) {
					this.page++
					this.getData()
				}
			},
			/**
			 * 刷新
			 */
			onRefresh() {
				this.page = 1;
				this.list = []
				this.getData()
			},
			/**
			 * 网络请求
			 */
			async getData() {
				let res = await getLiveUserList(storage.getLiveRoomInfo().id, this.page)
				this.total = res.data.list.total
				let list = res.data.list.data
				list.map((item, index) => {
					if (item.user.id === storage.getUserID()) {
						list.splice(index, 1)
					}
				})
				if (this.page == 1) {
					this.list = list
				} else {
					this.list = [...this.list, ...list]
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.title-bg {
		width: 300rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.title {
		color: #191919;
		font-size: 32rpx;
	}

	.list {
		margin-top: 30rpx;
		width: 750rpx;
		overflow: hidden;
		height: 600rpx;
	}

	.view {
		width: 750rpx;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}

	.item {
		width: 750rpx;
		padding: 0 35rpx;
		margin-bottom: 45rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		overflow: hidden;
	}

	.avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		overflow: hidden;
		background-color: red;
	}

	.nickname-bg {
		margin-left: 30rpx;
		margin-right: 30rpx;
		flex: 1;
		overflow: hidden;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.nickname {
		color: #1B1B1B;
		font-size: 30rpx;
	}

	.yy-lm {
		margin-left: 20rpx;
	}

	.lm-btn {
		height: 52rpx;
		border-radius: 26rpx;
		overflow: hidden;
		background: linear-gradient(to right, #4FB784, #A4D79F);
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0 27rpx;
	}

	.lm-title {
		color: white;
		font-size: 24rpx;
	}
</style>